package com.chang.dp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chang.dp.common.Result;
import com.chang.dp.entity.EbookEntity;
import com.chang.dp.services.impl.EbookServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Api(tags = "电子书")
@RestController
@RequestMapping("/api/ebook")
public class EbookController {

    @Resource
    private EbookServiceImpl ebookService;

    @ApiOperation("获取电子书信息")
    @GetMapping("/{id}")
    public Result<EbookEntity> getEbook(@PathVariable Long id) {
        return Result.success(ebookService.getById(id));
    }

    @ApiOperation("添加电子书")
    @PostMapping
    public Result<EbookEntity> addEbook(@RequestBody EbookEntity ebook) {
        ebookService.save(ebook);
//        ebookService.addSections(ebook);
        return Result.success(ebook);
    }

    @ApiOperation("修改电子书")
    @PutMapping
    public Result<Boolean> updateEbook(@RequestBody EbookEntity ebook) {
        return Result.success(ebookService.updateById(ebook));
    }

    @ApiOperation("删除电子书")
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteEbook(@PathVariable Long id) {
        return Result.success(ebookService.removeById(id));
    }

    @PostMapping("/list")
    public Result<Object> list(@RequestBody EbookEntity ebook) {
        return Result.success(ebookService.page(ebook.getPagination(), new QueryWrapper<>(ebook)));
    }

    @GetMapping("/analyze/{{id}}")
    public Result<Object> analyze(@PathVariable Long id) {
        return Result.success(ebookService.analyze(id));
    }
}
